<template>
  <div>
    <el-row :gutter="20">
      <el-col :span="12">
        <el-card>
          <template v-slot:header>
            <span>基础设置</span>
          </template>
          <el-form label-position="left" label-width="150px">
            <el-form-item v-for="item in typeMap.type1" :key="item.id" :label="item.nameZh">
              <el-input v-model="item.value"></el-input>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>
      <el-col :span="12">
        <el-card>
          <template v-slot:header>
            <span>资料卡</span>
          </template>
          <el-form label-position="left" label-width="150px">
            <el-form-item v-for="item in typeMap.type3" :key="item.id" :label="item.nameZh">
              <div v-if="item.nameEn === 'favorite'">
                <el-col :span="20">
                  <el-input v-model="item.value"></el-input>
                </el-col>
                <el-col :span="4">
                  <el-button icon="el-icon-delete" type="danger" @click="deleteFavorite(item)">
                    删除
                  </el-button>
                </el-col>
              </div>
              <div v-else>
                <el-input v-model="item.value"></el-input>
              </div>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>
    </el-row>
    <div style="text-align: right; margin-top: 30px">
      <el-button icon="el-icon-check" type="primary" @click="submit">保存</el-button>
    </div>
  </div>
</template>

<script>
  import { getSiteSettingData, update } from '@/api/siteSetting'
  import _ from 'lodash'

  export default {
    name: 'WebSiteSettings',
    components: {},
    data() {
      return {
        deleteIds: [],
        typeMap: {},
      }
    },
    created() {
      this.getData()
    },
    computed: {},
    methods: {
      getData() {
        getSiteSettingData().then((res) => {
          if (res.code === 200) {
            this.typeMap = res.data
          }
        })
      },
      submit() {
        const result = _.cloneDeep(this.typeMap)
        result.type2.forEach((item) => {
          item.value = JSON.stringify(item.value)
        })
        let updateArr = []
        updateArr.push(...result.type1)
        updateArr.push(...result.type2)
        updateArr.push(...result.type3)
        update(updateArr, this.deleteIds).then((res) => {
          if (res.code === 200) {
            this.deleteIds = []
            this.getData()
            this.msgSuccess(res.msg)
          }
        })
      },
    },
  }
</script>

<style lang="scss" scoped></style>
